package com.zzc.fwzpxt.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.Article;
import com.zzc.fwzpxt.entity.Company;
import com.zzc.fwzpxt.entity.Product;
import com.zzc.fwzpxt.entity.RegAddress;
import com.zzc.fwzpxt.service.ICompanyService;
import com.zzc.fwzpxt.service.IProductService;
import com.zzc.fwzpxt.service.IRegAddressService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.result.JsonResult;
import com.zzc.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品 product 前端控制器
 * </p>
 *
 * @author XiaoBingBy
 * @since 2017-09-28
 */
@Controller
@RequestMapping("/admin/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private ICompanyService iCompanyService;
    @Autowired
    private IRegAddressService iRegAddressService;

    /**
     * 文件列表 视图
     * @return
     */
    @GetMapping(value = "/list")
    public String list() {
        return blogPath + "product/list";
    }

    /**
     * 产品列表分页
     * @param pageNumber
     * @param pageSize
     * @param searchText
     * @return
     */
    @ResponseBody
    @PostMapping("/getList")
    public Map<String, Object> getUserList(int pageNumber, int pageSize, String searchText) {
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Product> page = new Page<>(pageNumber, pageSize);
        EntityWrapper<Product> wrapper = new EntityWrapper<>();
        if (StringUtil.isNotEmpty(searchText)) {
            wrapper.like("product_name", searchText);
        }
        wrapper.eq("status", 1);
        wrapper.orderBy("gmt_create", false);
        Page<Product> articlePage = iProductService.selectPage(page, wrapper);
        result.put("total", articlePage.getTotal());
        result.put("rows", articlePage.getRecords());
        return result;
    }

    /**
     * 产品列表 视图
     * @return
     */
    @GetMapping(value = "/addProduct")
    public String addProductView(Model model) {
        EntityWrapper<Company> companyEntityWrapper = new EntityWrapper<>();
        companyEntityWrapper.eq("status", 1);
        List<Company> companies = iCompanyService.selectList(companyEntityWrapper);

        EntityWrapper<RegAddress> regAddressEntityWrapper = new EntityWrapper<>();
        regAddressEntityWrapper.eq("status", 1);
        List<RegAddress> regAddresses = iRegAddressService.selectList(regAddressEntityWrapper);
        
        model.addAttribute("regAddresses", regAddresses);
        model.addAttribute("companies", companies);
        return blogPath + "product/add_product";
    }

    /**
     * 添加产品
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/addProduct")
    public JsonResult addProduct(Product product) {
        EntityWrapper<Product> wrapper = new EntityWrapper<>();
        wrapper.orderBy("product_code", false);
        Product maxCode = iProductService.selectOne(wrapper);
        if (maxCode == null) {
            product.setProductCode("JQ000000001");
        } else {
            String productCode = maxCode.getProductCode();
            String item = productCode.substring(2, productCode.length());
            Long code = Long.valueOf(item) + 1;
            product.setProductCode("JQ00000000"+code);
        }

        return iProductService.insert(product) ? renderSuccess("添加成功") : renderError("添加失败");
    }

    @GetMapping("/getOneProduct/{id}")
    public String getOneProduct(@PathVariable("id") Long id, Model model) {
        {
            EntityWrapper<Company> companyEntityWrapper = new EntityWrapper<>();
            companyEntityWrapper.eq("status", 1);
            List<Company> companies = iCompanyService.selectList(companyEntityWrapper);

            EntityWrapper<RegAddress> regAddressEntityWrapper = new EntityWrapper<>();
            regAddressEntityWrapper.eq("status", 1);
            List<RegAddress> regAddresses = iRegAddressService.selectList(regAddressEntityWrapper);

            model.addAttribute("regAddresses", regAddresses);
            model.addAttribute("companies", companies);
        }
        EntityWrapper<Product> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Product product = iProductService.selectOne(wrapper);
        model.addAttribute("product", product);
        return blogPath +"product/update_product";
    }

    /**
     * 修改产品
     * @param product
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/updateProduct")
    public JsonResult updateProduct(Product product) {
        return iProductService.updateById(product) ? renderSuccess("修改成功") : renderError("修改失败");
    }


    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestBody List<Product> products) {
        if (products == null) {
            return renderError("请选择数据");
        }
        products.forEach(a -> {
            a.setStatus(0);
        });

        return iProductService.updateBatchById(products)? renderSuccess("删除成功") : renderError("删除失败");
    }

}
